<template>
    <div class="app-container">
        <header-title/>
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="110px"
        >
            <el-form-item label="关键词" prop="keyword">
                <el-input
                    v-model="queryParams.keyword"
                    placeholder="请输入名字/编号"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="调用ai服务类型" prop="aiType">
                <el-select v-model="queryParams.aiType" clearable>
                    <el-option
                        v-for="dict in dict.type.ai_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
           
            <el-form-item label="创建时间" prop="jiTime">
                <el-date-picker
                    v-model="dateRange"
                    type="datetimerange"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    @keyup.enter.native="handleQuery"
                    placeholder="选择时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="最小扣除算力" prop="minDeductionPoint">
                <el-input
                    v-model="queryParams.minDeductionPoint"
                    placeholder="请输入最小扣除算力"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="最大扣除算力" prop="maxDeductionPoint">
                <el-input
                    v-model="queryParams.maxDeductionPoint"
                    placeholder="请输入最大扣除算力"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item class="mar-r-20">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="onHandleAdd">新增</el-button>

            </el-form-item>
        </el-form>
        <el-table
            v-loading="loading"
            :data="tableList"
        >   
        <!-- 订单ID，订单类型，用户手机号，调用服务，消耗算力，订单日期，订单状态 -->
            <el-table-column label="ID" >
                <template slot-scope="scope">
                    {{ scope.row.pointDeductionConfigId ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="AI服务类型" >
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.ai_type,scope.row.aiType ) ||'-'}}
                </template>
            </el-table-column>
            <el-table-column label="扣除算力" >
                <template slot-scope="scope">
                    {{ scope.row.deductionPoint || '-' }}
                </template>
            </el-table-column>
            
            <el-table-column label="创建时间" >
                <template slot-scope="scope">
                    {{ scope.row.createTime ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="更新时间" >
                <template slot-scope="scope">
                    {{ scope.row.updateTime ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="操作" >
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-search"
                        @click="handleUpdateDetail(scope.row,'detail')"
                    >详情</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdateDetail(scope.row,'edit')"
                    >修改</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="onHandleClickDel(scope.row.materialId)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        >
        </pagination>
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="type == 'detail' ? true : false">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  label="AI服务类型" prop="aiType">
                            <el-select v-model="form.aiType" >
                                <el-option
                                    v-for="dict in dict.type.ai_type"
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item  label="扣除算力" prop="deductionPoint">
                            <el-input v-model="form.deductionPoint" placeholder="请输入扣除算力"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer"  v-if="type != 'detail'">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import {listPointDeductionConfig,addPointDeductionConfig,detailPointDeductionConfig,deletePointDeductionConfig,updatePointDeductionConfig} from '@/api/computeOrderManage/computePower'

    export default{
        name:'ComputePower',
        dicts:['ai_type'],
        data() {
            return {
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,  
                    keyword:'',  
                    aiType:'',
                    endTime:'',
                    endTime:'',
                    maxDeductionPoint:'',
                    minDeductionPoint:'',
                    startTime:''               
                },
                showSearch: true,
                total: 0,
                dateRange: [],
                // 表格数据
                tableList: [],
                loading: false,
                title:'',
                open: false,
                form: {
                    aiType:'',
                    deductionPoint:''
                },
                rules: {
                    aiType: [
                        { required: true, message: "AI服务类型不能为空", trigger: "change" }
                    ],
                    deductionPoint: [
                        { required: true, message: "扣除算力不能为空", trigger: "change" }
                    ]
                },
                type:''
            }
        },
        created() {
            this.getList()
        },
        methods: {
            onHandleClickDel(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deletePointDeductionConfig(id).then(res => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList()
                    })
                   
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            handleUpdateDetail(row,type) {
                this.title ='算力扣除详情'
                
                this.type = type
                detailPointDeductionConfig({id: row.pointDeductionConfigId}).then(res=> {
                    this.form = res.data
                    this.open = true
                })
            },
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if(this.type=='edit') {
                            updatePointDeductionConfig(this.form).then(res=> {
                                this.$modal.msgSuccess(res.msg)
                                this.open = false
                                this.getList()
                            })
                        }else{
                            addPointDeductionConfig(this.form).then(res=> {
                                this.$modal.msgSuccess(res.msg)
                                this.open = false
                                this.getList()
                            })
                        }
                        
                    }
                })
            },
            onHandleAdd() {
                this.title="新增算力扣除配置"
                this.type = 'add'
                this.form =  {
                    aiType:'',
                    deductionPoint:''
                }
                this.open = true
            },
             /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            getList() {
                this.loading = true;
                if (this.dateRange && this.dateRange.length) {
                    this.queryParams.startTime = this.dateRange[0];
                    this.queryParams.endTime = this.dateRange[1];
                } else {
                    this.queryParams.startTime = undefined;
                    this.queryParams.endTime = undefined;
                }
               
                
                listPointDeductionConfig(this.queryParams).then((response) => {
                    this.tableList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                });
            }
        }
    }
</script>